Z.DeleteMulti = function (element) {
	try{
	for (var count = element.options.length - 1; count >= 0; count--) {
			
		
			var option = element.options[count];
			try {
				element.remove(count, null); // standard

			} catch (error) {

				element.remove(count); // IE
			}

		
	}
	}catch(e){
		alert("Z.DeleteMulti "+e.message)
	}
}



Z.ClearForm = function(oForm){
	try{
	    var elements = oForm.elements;
	    oForm.reset();
	    
	    for (i = 0; i < elements.length; i++) {
	    
	        field_type = elements[i].type;
	        switch (field_type) {
	        
	            case "text":
	            case "password":
	            case "textarea":
	            case "hidden":
	                
	                elements[i].value = "";
	                break;
	                
	            case "radio":
	            case "checkbox":
	                if (elements[i].checked) {
	                    elements[i].checked = false;
	                }
	                break;
	                
	            case "select-one":
	            case "select-multi":
	                elements[i].selectedIndex = -1;
	                break;
	                
	            default:
	                break;
	        }
	    }
	} catch (e) {
		alert("ClearForm: " + e );
	}
};

Z.ParsearError = function(oResponse) {

    try {
        var debug = false;
        if (debug) {
            alert("oResponse.error:" + oResponse.error);
            alert("oResponse.tId:" + oResponse.tId);
            alert("oResponse.status:" + oResponse.status);
            alert("oResponse.statusText:" + oResponse.statusText);
            alert("oResponse.getResponseHeader:" + oResponse.getResponseHeader);
            alert("oResponse.getAllResponseHeaders:" + oResponse.getAllResponseHeaders);
            alert("oResponse.responseText:" + oResponse.responseText);
            alert("oResponse.responseXML:" + oResponse.responseXML);
        }

        if (Y.lang.isArray(oResponse)) {
            var dump = "<br/>[";
            dump += Y.lang.dump(oResponse);
            dump += "]";
            if (oResponse.statusText) {
                if (oResponse.statusText.indexOf('communication failure') != -1) {
                    return "Servicio no disponible";
                } else {
                    return oResponse.statusText + dump;
                }
            }
        } else {
            var msg = "";
            if (oResponse.status) {
                msg += "<br/> Estatus: " + oResponse.status;
                if (oResponse.status == '404') {
                    msg += " La pagina solicitada no existe"
                }
            }
            if (oResponse.statusText) {
                if (oResponse.statusText.indexOf('communication failure') != -1) {
                    return "Servicio no disponible";
                } else {
                    msg += "<br/> [ " + oResponse.statusText + " ]";
                }

            }
            return msg;
        }
    } catch (e) {
        alert("ParsearError: " + e);
    }
};

Z.Publicar = function(divId, message) {
    try {

        var div = document.getElementById(divId);
        if (div != undefined) {
            var xx = Y.util.Dom.get(div);

            if (message == undefined || message == '') {
                Y.util.Dom.addClass(xx, 'info');
                Y.util.Dom.removeClass(xx, 'infovisible');
            } else {
                xx.innerHTML = message;
                Y.util.Dom.addClass(xx, 'infovisible');
                Y.util.Dom.removeClass(xx, 'info');
            }
        } else {
            alert(divId + " no existe en el documento.");
        }

    } catch (e) {
        txt = " message = [" + message + "] \n";
        txt += " divId = [" + divId + "] \n";
        txt += " e.description = [" + e.description + "]\n";
        alert("publishMsg:" + txt);
    }
};

Z.ProcesarDivs = function(data) {
    if (data.divs) {
        if (Y.lang.isArray(data.divs) == false) {
            alert("data.divs no es un array.");
            return;
        }
        for ( var j = 0; j < data.divs.length; j++) {
            var m = data.divs[j];
            Z.Publicar(m.div, m.msg);
        }
    }
};

Z.ShowWait = function() {
    if (!Z.Wait) {
        Z.Wait = new Y.widget.Panel("wait", {
            width : "240px",
            fixedcenter : true,
            close : true,
            draggable : false,
            zindex : 5,
            modal : true,
            visible : false
        });

        Z.Wait.setHeader("Procesando, por favor espere...");
        Z.Wait.setBody('<img src="' + Z.zuictx + '/loading.gif" />');
        Z.Wait.render(document.body);

        var kw = new Y.util.KeyListener(document, {
            keys : 27
        }, {
            fn : Z.Wait.hide,
            scope : Z.Wait,
            correctScope : true
        });
        kw.enable();

    }
    Z.Wait.show();
};

Z.Informar = function(mensaje) {
    if (!Z.InformarPanel) {
        Z.InformarPanel = new Y.widget.SimpleDialog("InformarPanel", {
            width : "240px",
            fixedcenter : true,
            close : true,
            draggable : false,
            zindex : 5,
            modal : true,
            visible : false,
            icon : Y.widget.SimpleDialog.ICON_INFO
        });

        Z.InformarPanel.setHeader("Informacion");
        Z.InformarPanel.setBody(mensaje);
        Z.InformarPanel.render(document.body);

        var kw = new Y.util.KeyListener(document, {
            keys : 27
        }, {
            fn : Z.InformarPanel.hide,
            scope : Z.InformarPanel,
            correctScope : true
        });
        kw.enable();

    }
    Z.InformarPanel.show();
};

Z.HideWait = function() {
    if (Z.Wait) {
        Z.Wait.hide();
    }
};

Z.ShowException = function(exception) {
    if (!Z.Exception) {
        Z.Exception = new Y.widget.Panel("exception", {
            width : "240px",
            fixedcenter : true,
            close : true,
            draggable : false,
            zindex : 5,
            modal : true,
            visible : false
        });

        Z.Exception.setHeader("Ocurrio un error...");
        Z.Exception.render(document.body);

        var kw = new Y.util.KeyListener(document, {
            keys : 27
        }, {
            fn : Z.Exception.hide,
            scope : Z.Exception,
            correctScope : true
        });
        kw.enable();

    }
    Z.Exception.setBody("<p>" + exception + "</p>");
    Z.Exception.show();
};

/**
 * Reemplaza los elementos option de un input select
 * 
 * @param jsonArray -
 *            Arreglo en nomenclatura JSON [ { "valor":"CLAVE1",
 *            "descripcion":"TEXTO CLAVE 1" }, { "valor":"CLAVE2",
 *            "descripcion":"TEXTO CLAVE 2" } ]
 * @param elSel -
 *            input select
 * @param text1st -
 *            valor para el primer elemento, donde el text sera una cadena vacia
 * @return
 */
Z.FillSelect = function(jsonArray, elSel, newOption) {
    try {
        if (Y.lang.isArray(jsonArray) == false) {
            alert('Result is not an array!');
            return;
        }
        if (elSel == undefined) {
            alert('elSel is undefined!');
            return;
        }

        if (newOption) {
            elSel.options.add(newOption);
        }

        for ( var j = 0; j < jsonArray.length; ++j) {
            elOptNew = new Option(jsonArray[j].descripcion, jsonArray[j].valor);
            elSel.options.add(elOptNew);
        }
        if (jsonArray.length == 1) {
            elSel.options[1].selected = true;
        }
        // elSel.disabled = false;
    } catch (e) {
        alert("clearAndFillSelect:" + e.description);
    }
};

Z.ClearSelect = function(elSel) {
    try {
        for (i = elSel.length - 1; i >= 0; i--) {
            elSel.remove(i);
        }
    } catch (e) {
        alert("ClearSelect:" + e.description);
    }
};

Z.Calendar = function() {

};

Z.Calendar.prototype = {
  BUTTON_ID : null,
  SHORT_DATE_ID : null,
  LARGE_DATE_ID : null,
  DIALOG_ID : null,
  CALENDAR_ID : 'tscalendarid',
  DIALOG_TITTLE : null,
  DIALOG : null,
  CALENDAR : null,
  MINDATE : '1/1/2010',
  CLOSE_EVENT : null,
  NAV_CONFIG : false,

  setButtonId : function(elementId) {
    this.BUTTON_ID = elementId;
  },
  setShortDateId : function(elementId) {
    this.SHORT_DATE_ID = elementId;
  },
  setLargeDateId : function(elementId) {
    this.LARGE_DATE_ID = elementId;
  },
  setDialogId : function(elementId) {
    this.DIALOG_ID = elementId;
  },
  setCalendarId : function(elementId) {
    this.CALENDAR_ID = elementId;
  },
  setDialogTitle : function(text) {
    this.DIALOG_TITTLE = text;
  },
  setMinDate : function(text) {
    this.MINDATE = text;
  },
  setCloseEvent : function(fn) {
    this.CLOSE_EVENT = fn;
  },  
  setNavConfig : function(text) {
    this.NAV_CONFIG = text;
  }, 
  construct : function() {
    var Event = Y.util.Event, Dom = Y.util.Dom;

    if (!this.BUTTON_ID) {
      alert("BUTTON_ID is undefined");
      return;
    }
    if (!this.SHORT_DATE_ID) {
      alert("SHORT_DATE_ID is undefined");
      return;
    }
    if (!this.LARGE_DATE_ID) {
      alert("LARGE_DATE_ID is undefined");
      return;
    }
    if (!this.DIALOG_ID) {
      alert("DIALOG_ID is undefined");
      return;
    }
    if (Dom.get(this.BUTTON_ID) == null) {
      alert("BUTTON_ID: El elemento " + this.BUTTON_ID + " no existe en el documento");
      return;
    }
    if (Dom.get(this.SHORT_DATE_ID) == null) {
      alert("SHORT_DATE_ID: El elemento " + this.SHORT_DATE_ID + " no existe en el documento");
      return;
    }
    if (Dom.get(this.DIALOG_ID) == null) {
      alert("DIALOG_ID: El elemento " + this.DIALOG_ID + " no existe en el documento");
      return;
    }
    if (Dom.get(this.LARGE_DATE_ID) == null) {
      alert("LARGE_DATE_ID: El elemento " + this.LARGE_DATE_ID + " no existe en el documento");
      return;
    }

    var _button_id = this.BUTTON_ID;
    var _short_date_id = this.SHORT_DATE_ID;
    var _large_date_id = this.LARGE_DATE_ID;
    var _dialog_id = this.DIALOG_ID;
    var _calendar_id = this.CALENDAR_ID;
    var _dialog_title = this.DIALOG_TITTLE;
    var _dialog = this.DIALOG;
    var _calendar = this.CALENDAR;
    var _mindate = this.MINDATE;
    var _close_event = this.CLOSE_EVENT;
    var showBtn = Dom.get(_button_id);
	var _navConfig = this.NAV_CONFIG;
	
    Event.on(showBtn, "click", function() {
      
      if (!_dialog) {
        Event.on(document, "click", function(e) {
          var el = Event.getTarget(e);
          var dialogEl = _dialog.element;
          if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != showBtn && !Dom.isAncestor(showBtn, el)) {
            _dialog.hide();
          }
        });
        function resetHandler() {
          Dom.get(_large_date_id).value = "";
          Dom.get(_short_date_id).value = "";
          _dialog.hide();
        }
        function closeHandler() {
          _dialog.hide();
        }
        _dialog = new Y.widget.Dialog(_dialog_id, {
          visible : false,
          context : [ _button_id, "tl", "bl" ],
          buttons : [ {
            text : "Limpiar",
            handler : resetHandler
          }, {
            text : "Cerrar",
            handler : closeHandler,
            isDefault : true
          } ],
          draggable : false,
          close : true,
          constraintoviewport : true,
          zIndex : 9000,
          height: '22em'
        });
        _dialog.setHeader(_dialog_title);
        _dialog.setBody('<div id="' + _calendar_id + '" style="border:none;padding:1em;"></div>');
        _dialog.render(document.body);
        _dialog.showEvent.subscribe(function() {
          if (Y.env.ua.ie) {
            _dialog.fireEvent("changeContent");
          }
        });
        if (_close_event == null) {
        } else {
          if (Y.lang.isFunction(_close_event)) {
            _dialog.hideEvent.subscribe( _close_event );
          } else {
            alert("_close_event no es una funcion.");
          }
        }        
      }
      if (!_calendar) {
	  	if (_navConfig) {
			var navConfig = {
                    strings : {
                        month: "Elige un mes",
                        year: "Elige un a&ntilde;o",
                        submit: "Aceptar",
                        cancel: "Cancelar",
                        invalidYear: "A&ntilde;o invalido"
                    },
                    monthFormat: Z.Calendar.SHORT_DATE_ID,
                    initialFocus: "year"
              };
			_calendar = new Y.widget.Calendar(_calendar_id, {
				iframe: false,
				hide_blank_weeks: true,
				mindate: _mindate,
				navigator: navConfig
			});
		} else {
			_calendar = new Y.widget.Calendar(_calendar_id, {
				iframe: false,
				hide_blank_weeks: true,
				mindate: _mindate
			});
		}

        _calendar.cfg.setProperty("MONTHS_SHORT", [ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ]);
        _calendar.cfg.setProperty("MONTHS_LONG", [ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre",
            "Octubre", "Noviembre", "Diciembre" ]);
        _calendar.cfg.setProperty("WEEKDAYS_SHORT", [ "Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa" ]);
        _calendar.cfg.setProperty("WEEKDAYS_MEDIUM", [ "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab" ]);
        _calendar.cfg.setProperty("WEEKDAYS_LONG", [ "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" ]);
        _calendar.render();

        _calendar.selectEvent.subscribe(function() {
          if (_calendar.getSelectedDates().length > 0) {
            var selDate = _calendar.getSelectedDates()[0];
            var wStr = _calendar.cfg.getProperty("WEEKDAYS_LONG")[selDate.getDay()];
            var dStr = selDate.getDate();
            var mStr = _calendar.cfg.getProperty("MONTHS_LONG")[selDate.getMonth()];
            var yStr = selDate.getFullYear();
            var mesStr = _calendar.cfg.getProperty("MONTHS_SHORT")[selDate.getMonth()];
            try {
              Dom.get(_large_date_id).value = wStr + ", " + dStr + " " + mStr + " " + yStr;
              Dom.get(_short_date_id).value = mesStr + "-" + selDate.getDate() + "-" +  selDate.getFullYear();
            } catch (e) {
              alert(e.description);
            }
          } else {
            Dom.get(_large_date_id).value = "";
          }
          _dialog.hide();
        });

        _calendar.renderEvent.subscribe(function() {
          _dialog.fireEvent("changeContent");
        });
      }

      var seldate = _calendar.getSelectedDates();

      if (seldate.length > 0) {
        _calendar.cfg.setProperty("pagedate", seldate[0]);
        _calendar.render();
      }

      _dialog.show();

    });

  }

};




